import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tencent_cloud_chat_sdk/models/v2_tim_conversation.dart';
import 'package:vischat/core/utils/custom_date_utils.dart';
import 'package:vischat/core/utils/tencent/conversation_util.dart';
import 'package:vischat/modules/home/socialize/socialize_controller.dart';
import 'package:vischat/shared/widgets/avatar.dart';

class ConversationItem extends GetView<SocializeController> {
  final V2TimConversation conversation;
  final bool isPinned;
  const ConversationItem({
    super.key,
    required this.conversation,
    required this.isPinned,
  });

  @override
  Widget build(BuildContext context) {
    const double avatarSize = 96;
    const double paddingHorizontal = 30;
    const double paddingVertical = 10;
    const double spacing = 20;
    const double titleFontSize = 32;
    const double subtitleFontSize = 24;

    final String lastMessage =
        ConversationUtil.getLastMessage(
          conversation.lastMessage,
        ) ??
        '';
    final String timeText = CustomDateUtils.formatTimestamp(
      conversation.lastMessage?.timestamp,
    );
    final int unreadCount = conversation.unreadCount ?? 0;

    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () => controller.goChat(conversation),
      child: Container(
        decoration: BoxDecoration(
          color: isPinned ? Colors.black.withAlpha(10) : Colors.white,
          border: Border(bottom: BorderSide(color: Colors.grey, width: 1.w)),
        ),
        padding: EdgeInsets.symmetric(vertical: paddingVertical.h),
        child: Container(
          padding: EdgeInsets.symmetric(horizontal: paddingHorizontal.w),
          child: Row(
            children: [
              Badge(
                label: Text('$unreadCount'),
                isLabelVisible: unreadCount > 0,
                child: SizedBox(
                  width: avatarSize.sp,
                  height: avatarSize.sp,
                  child: AppAvatar(avatar: conversation.faceUrl ?? ''),
                ),
              ),
              SizedBox(width: spacing.w),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          '${conversation.showName}',
                          style: TextStyle(fontSize: titleFontSize.sp),
                        ),
                        Text(
                          timeText,
                          style: TextStyle(
                            fontSize: subtitleFontSize.sp,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                    Text(
                      lastMessage,
                      style: TextStyle(
                        fontSize: subtitleFontSize.sp,
                        color: Colors.grey,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
